﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OreSystem.BLL;
using OreSystem.Entities;
namespace OreSystemWeb.Areas.Admin.Controllers
{
    public class LevelController : Controller
    {
        private LevelBll levelBll = new LevelBll();
        //
        // GET: /Admin/Level/

        public ActionResult Index(int? id)
        {
            IList<Level> levels = levelBll.GetLevelByLevel((int)id - 1);
            if (levels.Count <= 0)
            {
                levels.Add(new Level { ID = 0, Name = "顶层" });
            }
            var items = from a in levels select new SelectListItem { Text = a.Name, Value = a.ID.ToString() };
            //ViewData["PID"] = new SelectList(new[] { new { ID = 0, Name = "顶层" } }, "ID", "Name");
            ViewData["PID"] = items;
            ViewData["layer"] = id;
           return View();
        }
        //
        // GET: /Admin/Level/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Admin/Level/Create

        public ActionResult Create(int id)
        {
            IList<Level> levels = levelBll.GetLevelByLevel((int)id - 1);
            if (levels.Count <= 0)
            {
                levels.Add(new Level { ID = 0, Name = "顶层" });
            }
            var items = from a in levels select new SelectListItem { Text = a.Name, Value = a.ID.ToString() };
            ViewData["PID"] = items;
            //ViewData["PID"] = new SelectList(items, "ID", "Name");            
            return Json(new OreSystem.Entities.Level() {Layer=id, CreateDate=DateTime.Now}, JsonRequestBehavior.AllowGet);
        } 

        //
        // POST: /Admin/Level/Create

        [HttpPost]
        public ActionResult Create(Level level)
        {
            try
            {
                level.Path = level.PID.ToString();
                level.CreateDate = DateTime.Now;
                levelBll.SaveLevel(level);
                return Json(new { success=true });
            }
            catch(Exception e)
            {
                return Json(new { msg =e.Message  });
            }
        }

        public ActionResult GetListByPage(int id, int page=1, int rows=10)
        {
            var lists = levelBll.GetListByPage(id, page - 1, rows);
            int total = (int)levelBll.GetLevelByLevel(id).Count();
            if (lists.Count <= 0)
            {
                lists = levelBll.GetListByPage(id, 1, rows);
            }
            return Json(new { total = total, rows = lists }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GetAll(int id)
        {
            return Json(levelBll.GetLevelByLevel(id),JsonRequestBehavior.AllowGet);
        }
        
        //
        // GET: /Admin/Level/Edit/5
        
 
        public ActionResult Edit(int id)
        {            
            Level level = levelBll.GetLevelById(id);
            return Json(level,JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Admin/Level/Edit/5

        [HttpPost]
        public ActionResult Edit(Level level)
        {
            try
            {
                // TODO: Add update logic here

                levelBll.Update(level);
                return Json(new { success = true, msg = "保存成功" });
            }
            catch(Exception e)
            {
                return Json(new {success=false,msg=e.Message });
            }
        }

        //
        // GET: /Admin/Level/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Admin/Level/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                levelBll.DeleteByID(id);
                return Json(new {success=true,msg="删除成功" });
            }
            catch(Exception e)
            {
                return Json(new {success=false, msg =e.Message  });
            }
        }
        public ActionResult Deletes(IList<int> idlist)
        {
            try
            {
                // TODO: Add delete logic here
                //levelBll.DeleteByID(id);
                levelBll.deletes(idlist.Cast<object>().ToList());
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception e)
            {
                return Json(new { success = false, msg = e.Message });
            }
        }
    }
}
